<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
	<%@ taglib prefix='sec' uri='http://www.springframework.org/security/tags'%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
	<script type="text/javascript" src="scripts/boot.js"></script>
	<style type="text/css">
		.New_Button, .Edit_Button, .Delete_Button, .Update_Button, .Cancel_Button
	    {
	        font-size:11px;color:#1B3F91;font-family:Verdana;  
	        margin-right:5px;
	    }
	    .mini-radiobuttonlist-td
	    {
	    	height: 25px;
	    }
	    .asLabel .mini-textbox-border,
	    .asLabel .mini-textbox-input,
	    .asLabel .mini-buttonedit-border,
	    .asLabel .mini-buttonedit-input,
	    .asLabel .mini-textboxlist-border
	    {
	        border:0;background:none;cursor:default;
	    }
	    .asLabel .mini-buttonedit-button,
	    .asLabel .mini-textboxlist-close
	    {
	        display:none;
	    }
	    .asLabel .mini-textboxlist-item
	    {
	        padding-right:8px;
	    } 
	</style>
</head>
<body>
	<fieldset style="border:solid 1px #aaa;padding:3px;">
		<legend >客户电话</legend>
        <div id="form1" style="padding-left:5px;padding-bottom:7px;">
	        <input name="callId" class="mini-hidden" />
	        <input name="callStatus" class="mini-hidden" />
            <div id="datagrid1" class="mini-datagrid" style="width:720px;height:auto;" showLoading="false" allowResize="false"
        		url="tel_info_view.do"  idField="id" showPager="false">
		        <div property="columns">
		            <div field="phoneNum" width="13%" headerAlign="center" align="center">号码
		                <input property="editor" emptyText="请输入号码" name="phoneNum" class="mini-textbox" style="width:100%;" required="true" vtype="int;rangeChar:7,12;numType" />
		            </div>
		            <div type="comboboxcolumn" field="type" width="11%" headerAlign="center" align="center">类型
		                <input property="editor" showNullItem="true" nullItemText="全部" emptyText="请选择..." name="type" valueField="keyName" textField="keyValue" class="mini-combobox" style="width:100%;" url="dictionary_cm_key_val_view.do?typeName=CUSTOMER_PHONE_TYPE" required="true"/>                
		            </div>
		            <div type="comboboxcolumn" field="isMain" width="11%" headerAlign="center" align="center">是否主要
		            	<input property="editor" showNullItem="true" nullItemText="全部" emptyText="请选择..." name="isMain" class="mini-combobox" style="width:100%;" data="isMajor" required="true"/>
		            </div>
		            <div field="remarks" width="29%" headerAlign="center" align="center">备注
		                <input property="editor" emptyText="请输入备注" name="remarks" class="mini-textbox" style="width:100%;"/>
		            </div>                              
		            <sec:authorize url='/customer_call_info_setting.do'>
		            <div name="action" width="20%" headerAlign="center" align="center" renderer="onActionRenderer" cellStyle="padding:0;">操作</div>
		            </sec:authorize>
		        </div>
    		</div>
        </div>
        <div id="win—outgoing" class="mini-window" showModal="false" allowDrag="false" showHeader="false" title="呼出" style="width: 200px;height: 110px;">
		    <input labelField="true" label="号码：" labelStyle="width: 40px;" class="mini-textbox" style="width:170px;margin-top: 7px;margin-left: 5px;" id="outNumId" enabled="false"/> 
		    <input labelField="true" label="加拨：" labelStyle="width: 40px;" class="mini-combobox" style="width:170px;margin-top: 7px;margin-left: 5px;" id="addNum"
		    	url="dictionary_cm_key_val_view.do?typeName=CALL_ADD_TYPE" textField="keyValue" valueField="keyValue" showNullItem="true" nullItemText="无"/> 
			<div style="text-align:center;padding-bottom: 5px;margin-top: 10px;">
				<a class="mini-button" onclick="outgoingOk" style="">呼出</a>
				<a class="mini-button" onclick="onClose2" style="margin-left: 30px;">关闭</a>
			</div>
		</div>
    </fieldset>
    <form id="form2" method="post">
	    <fieldset style="border:solid 1px #aaa;padding:3px;">
	        <legend >基本信息</legend>
	        <div style="padding:10px;">
	        <input name="id" id="customer_id" class="mini-hidden" />
			    <table cellspacing="15px;" cellpadding="0px;">
			        <tr>
			            <td align="right">姓名：</td>
			            <td style="width:250px;">    
			                <input name="name" emptyText="请输入姓名" style="width: 150px;" class="mini-textbox" required="true"/>
			            </td>
			            <td align="right">性别：</td>
			            <td >                        
			                <input name="sex" class="mini-radiobuttonlist" repeatItems="1" repeatLayout="table" repeatDirection="vertical" textField="keyValue" valueField="keyName"
			                	url="dictionary_cm_key_val_view.do?typeName=CUSTOMER_SEX_TYPE" onbeforeshowpopup="onbeforeshowpopup" value="10"/>
			            </td>
			        </tr>
			        <tr>
			            <td align="right">出生日期：</td>
			            <td >
			                <input name="birthDate" showTodayButton="false" style="width: 150px;" class="mini-datepicker" emptyText="请选择..."/>
			            </td>
			            <td align="right">客户级别：</td>
			            <td >    
<!-- 			                <input name="customerLevel" style="width: 150px;" class="mini-combobox" valueField="keyName" textField="keyValue" -->
<!-- 			                 data-options="{_url:'customer_find_key.do?keyType=CUSTOMER_LEVEL_TYPE&keyPid=0'}" onbeforeshowpopup="onbeforeshowpopup"/> -->
			                <input name="customerLevel" emptyText="请选择..." showNullItem="true" nullItemText="全部" style="width: 150px;" class="mini-combobox" valueField="keyName" textField="keyValue"
			                 	url="dictionary_cm_key_val_view.do?typeName=CUSTOMER_LEVEL_TYPE" value="1"/>
			            </td>
			        </tr>
			        <tr>
			        	<td align="right">客户来源：</td>
			        	<td>
			        		<input showNullItem="true" emptyText="请选择..." nullItemText="全部" name="customersSource" id="customers_source" style="width: 150px;" valueField="keyName" textField="keyValue" class="mini-combobox"
			        		 url="dictionary_cm_key_val_view.do?typeName=CUSTOMER_SOURCE_TYPE" value="33"/>
			        	</td>
			        	<td align="right">是否邮寄期刊：</td>
			        	<td>
			        		<input name="journal" emptyText="请选择..." showNullItem="true" nullItemText="全部" style="width: 150px;" valueField="keyName" textField="keyValue" class="mini-combobox"
			        		 url="dictionary_cm_key_val_view.do?typeName=CUSTOMER_MAIL_TYPE" value="14"/>
			        	</td>
			        </tr>
			        <tr>
			        	<td align="right">地址：</td>
			        	<td colspan="3">
			        		<input name="provinceId" id="province_val" class="mini-hidden"/>
			        		<input id="province" name="pName" class="mini-textbox" onfocus="focusP" emptyText="请选择省份"/>
			        		<input name="cityId" class="mini-hidden" id="city_val" />
			        		<input class="mini-textbox" name="cName" id="city" readOnly="true" emptyText="请选择城市"/>
			        		<input name="districtId" id="district_val" class="mini-hidden"/>
			        		<input class="mini-textbox" name="dName" id="district" readOnly="true" emptyText="请选择区县"/>
			        		<span>邮编：</span>
			        		<input emptyText="请先选择地址" class="mini-textbox" name="zipCode" style="width: 85px;" />
			        	</td>
			        </tr>
			        <tr>
			        	<td align="right">详细地址：</td>
			        	<td colspan="3">
			        		<input name="address" emptyText="请输入详细地址" class="mini-textbox" style="width:384px;" />
			        	</td>
			        </tr>
			        <tr>
			        	<td align="right">会员信息：</td>
			            <td colspan="3"> 
			            	<span>余额：</span>   
			                <input name="money" emptyText="请输入余额" class="mini-textbox" vtype="float;maxLength:11" value="0" style="width: 85px;"
			                <sec:authorize url="/customer_money_read.do">enabled="true"</sec:authorize> enabled="false"/>
			                <span>积分：</span>
			                <input name="integral" emptyText="请输入积分" class="mini-textbox" vtype="int;maxLength:11" value="0" style="width: 85px;"
			                <sec:authorize url="/customer_integral_read.do">enabled="true"</sec:authorize> enabled="false"/>
			                <span>红包：</span>
			                <input name="redEnvelopes" emptyText="请输入红包" class="mini-textbox" vtype="int;maxLength:11" value="0" style="width: 85px;"
			                <sec:authorize url="/customer_red_envelopes_read.do">enabled="true"</sec:authorize> enabled="false"/>
			            </td>
			        </tr>
			        <tr>
			            <td align="right">推荐人：</td>
			            <td>
			            	<input name="recommendId" emptyText="请选择..." style="width: 150px;" class="mini-buttonedit" onbuttonclick="onButtonEdit"/>
			            </td>
			        </tr>
			        <tr>
			            <td align="right">备注：</td>
			            <td colspan="3">
			                <input name="remarks" emptyText="请输入备注" class="mini-textarea" style="width:384px;" />
			            </td>
			        </tr>
			    </table>
	        </div>
	    </fieldset>
	    <div style="text-align:center;padding-top:15px;">         
	    	<sec:authorize url='/customer_save.do'>      
	        <a class="mini-button" onclick="onOk" style="width:100px;margin-right:20px;">保存客户信息</a> 
	        </sec:authorize>      
	<!--         <a class="mini-button" onclick="onCancel" style="width:60px;">取消</a>        -->
	    </div>
	</form>
	<div id="win1" class="mini-window" allowDrag="false" showHeader="false" title="选择地址—省份" style="width: 300px;height: 150px;">
	</div>
	<div id="win2" class="mini-window" allowDrag="false" showHeader="false" title="选择地址—省份" style="width: 300px;height: 150px;">
	</div>
	<div id="win3" class="mini-window" allowDrag="false" showHeader="false" title="选择地址—省份" style="width: 300px;height: 150px;">
	</div>
</body>
</html>

<script type="text/javascript">
	mini.parse();
	var grid = mini.get("datagrid1");
	
	// 懒加载下拉数据
// 	function onbeforeshowpopup(e) {
// 		var url = e.sender._url;
//         if (url && !e.sender._loaded) {
//         	e.sender.setUrl(url);
//         	e.sender._loaded = true;
//         }
// 	}
	
	// 加载本地数据
	var isMajor = [{ id: 1, text: '是' }, { id: 0, text: '否'}];
 	
 	// 标准方法接口定义
 	var callId = mini.getbyName("callId");
 	var callStatus = mini.getbyName("callStatus");
	function SetData(data) {
		data = mini.clone(data);
		grid.load({callId: data.id});
		callId.setValue(data.id);
		callStatus.setValue(data.callStatus);
		$.post("call_manage_my_unt_find_customer.do", {id: data.id}, function(data){
			if(null != data.customer) {
				form2.setData(data.customer);
				top["win"].setEnable(data.customer.id, data.customer.name, data.customer.callId);
			}
		}, "json");
	}
    
    function onActionRenderer(e) {
        var grid = e.sender;
        var record = e.record;
        var uid = record._uid;
		
        var s = '<a class="New_Button" href="javascript:newRow()">添加</a>'
                + ' <a class="Edit_Button" href="javascript:editRow(\'' + uid + '\')" >修改</a>'
                + ' <a class="Delete_Button" href="javascript:delRow(\'' + uid + '\')">删除</a>'
                + '	<a class="mini-button" iconCls="icon-add" style="width: 40px;" onclick="outgoing(\'' + uid + '\')" >外呼</a>';

        if (grid.isEditingRow(record) && record.id == undefined) {
            s = '<a class="Update_Button" href="javascript:saveRow(\'' + uid + '\')">保存</a>'
                + '<a class="Cancel_Button" href="javascript:cancelRow(\'' + uid + '\')">取消</a>';
        }else if (grid.isEditingRow(record) && record.id != undefined){
            s = '<a class="Update_Button" href="javascript:updateRow(\'' + uid + '\')">更新</a>'
                + '<a class="Cancel_Button" href="javascript:cancelRow(\'' + uid + '\')">取消</a>';
        }
        
        return s;
    }
	
    function newRow() {     
    	if(grid.isEditing()) {
    		mini.alert("请先保存号码！");
    	}else {
    		if(grid.getData().length == 4) {
    			mini.alert("每个客户最多可添加四个电话号码！");
    		}else {
		        var row = {};
		        grid.addRow(row, 0);
		        grid.beginEditRow(row);
    		}
    	}
    }
    
    function editRow(row_uid) {
        var row = grid.getRowByUID(row_uid);
        if(grid.isEditing()) {
        	mini.alert("请先保存号码！");
        }else {
	        if (row) {
	            grid.cancelEdit();
	            grid.beginEditRow(row);
	        }
        }
    }
    
    function cancelRow(row_uid) {            
        grid.reload();
    }
    
    function delRow(row_uid) {
        var row = grid.getRowByUID(row_uid);
        if(grid.isEditing()) {
    		mini.alert("请先保存号码！");
   		}else {
   			if(grid.getData().length > 1) {
   				if (row) {
   		            if (confirm("确定删除此记录？")) {
   		            	if(row.isMain == 0) {
   		            		grid.loading("删除中，请稍后......");
   	   		                $.ajax({
   	   		                    url: "tel_info_del.do?id=" + row.id,
   	   		                    dataType: "json",
   	   		                    success: function (text) {
   	   		                    	if(text.success){
   	   			                        grid.reload();
   	   		                   		}else{
   	   		                   			mini.alert("删除失败，id不能为空。");
   	   		                   		}
   	   		                    }
   	   		                });
   		            	} else {
   		            		mini.alert("主要号码不能删除，请修改后删除！");
   		            	}
   		            }
   		        }
   			}else{
   				mini.alert("至少应保留1个号码！");
   			}
        }
    }
    
/*连接WebSocket*/
    jQuery.openWebSocket("ws://172.16.2.11:8080/websocket");
    
// 连接成功时执行
    function webSocketOnOpen(event) {
		
 	}
    
// 外呼
    var winOutgoing = mini.get("win—outgoing");
    var outNumId = mini.get("outNumId");
    var addNum = mini.get("addNum");
    function outgoing(row_uid) {
    	var row = grid.getRowByUID(row_uid);
		winOutgoing.showAtPos("right", "top");
		outNumId.setValue(row.phoneNum);
    }
    
    function outgoingOk() {
    	$.post("alternately_cur_extension.do", function(data) {
    		if(null != data.phoneNum && 0 != data.phoneNum && data.phoneNum.length > 0) {
    			var arr = new Array();
    			arr[0] = addNum.getValue();
    			arr[1] = outNumId.getValue();
    			var callNum = arr.join("");
    			jQuery.dail(data.phoneNum, callNum);
    			winOutgoing.hide();
    			outNumId.setValue("");
    			addNum.setValue("");
    		} else {
    			mini.alert("未登录分机");
    		}
    	}, "json")
    }
    
    function onClose2() {
    	winOutgoing.hide();
    	outNumId.setValue("");
    	addNum.setValue("");
    }
	
    // 添加验证号码是否重复
    function checkNum(row_index, phoneNum) {
    	var data = grid.getData();
    	var j = 0;
    	data.splice(row_index, 1);
    	for(var i in data){
	    	if(phoneNum == data[i].phoneNum) {
	    		j++;
	    	}
    	}
    	return j;
    }
    
    // 验证号码格式
   	mini.VTypes["numTypeErrorText"] = "号码格式不正确";
    mini.VTypes["numType"] = function (v) {
    	var reMobile = new RegExp("^0?1[34578]\\d{9}$");
    	var rePhone = new RegExp("^(010|02\\d|0[3-9]\\d{2})?\\d{7,8}$");
    	if(reMobile.test(v)) {
    		return true;
    	} else if(rePhone.test(v)) {
    		return true;
    	} else {
    		return false;
    	}
    }
    
    var form1 = new mini.Form("#form1");
    function saveRow(row_uid) {
        var row = grid.getRowByUID(row_uid);
        var o = form1.getData(true);
        form1.validate();
	    if (form1.isValid() == false) return;
	    var i = checkNum(grid.indexOf(row), o.phoneNum);
	    if(i == 1) {
	    	mini.alert("号码已存在，请重新添加！");
	    }else {
	    	if(validNumUnique(o.phoneNum).length > 0) {
	    		
	    		$.post("call_manage_my_unt_find_customer.do", {id: callId.getValue()}, function(data) {
	    			
	    			if(null != data.customer) {	// 当前打开的任务包含客户信息
	    				mini.confirm("您输入的手机号已经存在另一个客户，要合并客户信息吗？如果合并客户信息，将会清除旧的客户资料！", "合并任务提示",function (action) {
	    					if (action == "ok") {	    						
	    						
	    						// 合并所有信息到老任务中，删除当前打开的任务。
	    						$.post("call_merge_call.do", {
	    							callStatus: callStatus.getValue(), 
	    							id: callId.getValue(), 
	    							customerId: data.customer.id, 
	    							openCallId: validNumUnique(o.phoneNum)}, 
	    							function(data) {
		    							if(data.success) {
		    								// 加载老任务数据
			    							$.post("call_find_status.do", {id: validNumUnique(o.phoneNum)}, function(data) {
				    							var callData = new Object;
				    							callData.callStatus = data.callStatus;
				    							callData.id = data.callId;
				    							SetData(callData);
				    						}, "json");
		    							}
	    				    	}, "json");
	    					}
	    	    		});
	    			} else {					// 不包含客户信息
	    				mini.confirm("您输入的手机号找到客户信息，打开客户吗？", "打开客户信息提示", function (action) {
	    					if (action == "ok") {
	    						// 合并号码到老任务中，删除当前打开的任务。
	    						$.post("call_merge_phone.do", {callStatus: callStatus.getValue(), id: callId.getValue(), openCallId: validNumUnique(o.phoneNum)}, function(data) {
	    							if(data.success) {
	    								// 加载老任务数据
		    							$.post("call_find_status.do", {id: validNumUnique(o.phoneNum)}, function(data) {
			    							var callData = new Object;
			    							callData.callStatus = data.callStatus;
			    							callData.id = data.callId;
			    							SetData(callData);
			    						}, "json");
	    							}
	    				    	}, "json");
	    					}
	    	    		});
	    			}
	    		}, "json");
	    		
	    	} else {
	    		if(checkMainNum(o.phoneNum, o.isMain)) {
		    		grid.loading("保存中，请稍后......");
	    			$.ajax({
				        url: "tel_info_save.do?customerId=" + customerId.getValue(),
						type: 'post',
						dataType: 'json',
				        data: o,
				        cache: false,
				        success: function (text) {
				            if(text.success){
				            	grid.commitEditRow(row);
				            	grid.reload();
				            }else{
				            	mini.alert("数据错误！");
				            }
				        },
				        error: function (jqXHR, textStatus, errorThrown) {
				            alert(jqXHR.responseText);
				            window.CloseOwnerWindow();
				        }
				    });
	    		} else {
	    			mini.alert("主要号码已存在！");
	    		}
	    	}
	    }
    }
    
    function updateRow(row_uid) {
        var row = grid.getRowByUID(row_uid);
        var o = form1.getData(true);
        form1.validate();
	    if (form1.isValid() == false) return;
	    var i = checkNum(grid.indexOf(row), o.phoneNum);
	    if(i == 1) {
	    	mini.alert("号码已存在，请重新修改！");
	    }else {
	        if(mustHaveMain(o.phoneNum, o.isMain)) {
	        	if(updateCheckMainNum(row.id, o.isMain)) {
	        		grid.loading("更新中，请稍后......");
		        	$.ajax({
				        url: "tel_info_edit.do?id=" + row.id,
						type: 'post',
						dataType: 'json',
				        data: o,
				        cache: false,
				        success: function (text) {
				            if(text.success){
				            	grid.commitEditRow(row);
				            	grid.reload();
				            }else{
				            	mini.alert("数据错误！(id不能为空)");
				            }
				        },
				        error: function (jqXHR, textStatus, errorThrown) {
				            alert(jqXHR.responseText);
				            window.CloseOwnerWindow();
				        }
				    });
	        	} else {
	        		mini.alert("主要号码已存在");
	        	}
	        } else {
	        	mini.alert("必须保留主要号码！");
	        } 
	    }
    }
    
    // 验证主要号码是否已存在
    function checkMainNum(phoneNum, isMain) {
    	var data = grid.getData();
    	var flag = false;
    	if(isMain == 1) {
    		if(data.length > 1) {
		    	for(var i in data) {
		    		if(data[i].isMain == 1) {
		    			flag = true;
		    			break;
		    		}
		    	}
    		}
	    	if(flag) {
    			return false;
    		} else {
    			return true;
    		}
    	} else {
    		return true;
    	}
    }
    
    // 更新验证主要号码是否已存在
    function updateCheckMainNum(id, isMain) {
    	var data = grid.getData();
    	var flag = false;
    	if(isMain == 1) {
    		if(data.length > 1) {
		    	for(var i in data) {
		    		if(data[i].id != id && data[i].isMain == 1) {
		    			flag = true;
		    			break;
		    		}
		    	}
    		}
	    	if(flag) {
    			return false;
    		} else {
    			return true;
    		}
    	} else {
    		return true;
    	}
    }
    
    // 验证主要号码必须存在
    function mustHaveMain(phoneNum, isMain) {
    	var data = grid.getData();
    	var flag = false;
    	if(isMain == 0) {
    		if(data.length > 1) {
	    		for(var i in data) {
	    			if(data[i].phoneNum != phoneNum && data[i].isMain == 1) {
	    				flag = true;
	    				break;
	    			}
	    		}
    		}
    		if(flag) {
    			return true;
    		} else {
    			return false;
    		}
    	} else {
    		return true;
    	}
    }
    
    // 验证号码是否已存在其他任务中
    function validNumUnique(phoneNum) {
    	var callId;
    	$.ajax({
  	    	url: "tel_valid_unique.do?phoneNum=" + phoneNum,
  	    	dataType: 'json',
  	        cache: false,
	    	async: false,
 	        success: function (data) {
 	            if(data.success){
 	            	callId = "";
 	            }else{
 	            	callId = data.callId;
 	            }
 	        }
  	    });
    	return callId;
    }
    
 	// 客户信息表单
    var form2 = new mini.Form("form2");
 	
 	// 弹出省份列表
 	var win1 = mini.get("win1");
 	var province = mini.get("province");
 	var provinceVal = mini.get("province_val");
 	function focusP(e) {
 		var el=e.sender.getEl();
		var bodyEl1 = win1.getBodyEl();
		$(bodyEl1).empty();
		$.post("customer_manage_find_province.do", {}, function(data) {
			win1.showAtEl(el, {
			   xAlign:"left",
			   yAlign:"below"
			});
			$.each(data, function(i, obj) {
				$(bodyEl1).append("</li><a href='javascript:loadC("+obj.id+", "+'"'+obj.pName+'"'+")' style='padding: 5px;'>"+obj.pName+"</a>");
			});
		}, "json");
		
 	}
 
 	// 通过省加载城市
 	var city = mini.get("city");
 	var cityVal = mini.get("city_val");
 	var win2 = mini.get("win2");
 	function loadC(p_id, p_name) {
 		var el = city.getEl();
		var bodyEl2 = win2.getBodyEl();
		$(bodyEl2).empty();
		$.post("customer_manage_find_city.do", {provinceId: p_id}, function(data) {
	 		provinceVal.setValue(p_id);
	 		province.setValue(p_name);
	 		win1.hide();
			win2.showAtEl(el, {
			   xAlign:"left",
			   yAlign:"below"
			});
			$.each(data, function(i, obj) {
				$(bodyEl2).append("<a href='javascript:loadD("+'"'+obj.id+'"'+", "+'"'+obj.cName+'"'+", "+'"'+obj.zipCode+'"'+")' style='padding: 5px;'>"+obj.cName+"</a>");
				mini.parse();
			});
		}, "json");
 	}
 	
 	// 通过城市加载区县
 	var win3 = mini.get("win3");
 	var district = mini.get("district");
 	var districtVal = mini.get("district_val");
 	var zipCode = mini.getbyName("zipCode");
 	function loadD(c_id, c_name, c_zipCode) {
 		var el = district.getEl();
 		var bodyEl3 = win3.getBodyEl();
 		zipCode.setValue(c_zipCode);
 		$(bodyEl3).empty();
 		$.post("customer_manage_find_district.do", {cityId: c_id}, function(data) {
 			city.setValue(c_name);
 			cityVal.setValue(c_id);
 			win2.hide();
 			win3.showAtEl(el, {
 			   xAlign:"left",
 			   yAlign:"below"
 			});
 			$.each(data, function(i, obj) {
				$(bodyEl3).append("<a href='javascript:setD("+obj.id+", "+'"'+obj.dName+'"'+")' style='padding: 5px;'>"+obj.dName+"</a>");
				mini.parse();
			});
 		}, "json");
 	}
 	
 	// 设置区县
 	function setD(d_id, d_name) {
 		win3.hide();
 		districtVal.setValue(d_id);
 		district.setValue(d_name);
 	}
 	
 	// 弹出选择推荐人
 	function onButtonEdit(e) {
        var btnEdit = this;
        mini.open({
            url: "customer_manage_choose_rec.do",
            title: "选择推荐人",
            width: 450,
            height: 350,
            allowResize: false,
            onload: function () {
                var iframe = this.getIFrameEl();
                var data = { id: customerId.getValue() };
                iframe.contentWindow.SetData(data);
            },
            ondestroy: function (action) {
                //if (action == "close") return false;
                if (action == "ok") {
                    var iframe = this.getIFrameEl();
                    var data = iframe.contentWindow.GetData();
                    data = mini.clone(data);
                    if (data) {
                        btnEdit.setValue(data.id);
                        btnEdit.setText(data.name);
                    }
                }
            }
        });            
    }
 	
    var customerId = mini.get("customer_id");
    function onOk() {
    	if(grid.isEditing()) {
    		mini.alert("请先保存号码！");
    	} else {
    		var o = form2.getData(true); 
    	    form2.validate();
    	    if (form2.isValid() == false) return;
    	    var url = "";
    	    if(customerId.getValue().length > 0) {
    	    	url = "customer_edit.do";
    	    }else {
    	    	url = "customer_save.do?callId=" + callId.getValue();
    	    }
    	    $.ajax({
    	        url: url,
    			type: 'post',
    			dataType: 'json',
    	        data: o,
    	        cache: false,
    	        success: function (text) {
    	            if(text.success){
    	            	if(text.customerId) {
    	            		top["win"].setEnable(text.customerId, o.name, callId.getValue());
    	            		customerId.setValue(text.customerId);
	    	            	mini.alert("保存成功！");
    	            	} else {
    	            		mini.alert("修改成功！");
    	            	}
    	            }else{
    	            	mini.alert("数据错误！");
    	            }
    	        },
    	        error: function (jqXHR, textStatus, errorThrown) {
    	            alert(jqXHR.responseText);
    	            window.CloseOwnerWindow();
    	        }
    	    });
    	}
    }
	
</script>